from django.http import HttpResponse, Http404, HttpResponseRedirect
from nameremoved.comics.models import Contribution, Contributor
from django import newforms as forms
from nameremoved.helpers import render_with_requestcontext

ContributionForm = forms.form_for_model(Contribution, fields=('aka', 'content'))

def index(request):
    c = {
        'current': '/contribute/',
        'contributors': Contributor.objects.all(),
        'other': Contribution.objects.filter(
            flagged=True,
            contributor__isnull=True
        ).extra(select={'lower_aka': 'lower(aka)'}).order_by('lower_aka'),
    }
    return render_with_requestcontext('contribution/default.html',c,request)

def submit(request):
    if request.method == 'POST':
        form = ContributionForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/contribute/thanks/')
    else:
        form = ContributionForm()
    return render_with_requestcontext('contribution/submit.html',
                                      {'form':form}, request)

def thanks(request):
    return render_with_requestcontext('contribution/thanks.html',{},request)
